package com.pj.point.pri;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.pj.point.R;

public class SettingPrivatePwd extends AppCompatActivity {

    private EditText priPwdEt;
    private EditText priPedAgainEt;
    private Button savePriPwdBt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_private_pwd);
        ActionBar bar = getSupportActionBar();
        bar.setTitle("设置抽屉密码");
        bar.setDisplayHomeAsUpEnabled(true);
        initView();

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        finish();
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        priPwdEt = (EditText) findViewById(R.id.pri_pwd_et);
        priPedAgainEt = (EditText) findViewById(R.id.pri_ped_again_et);
        savePriPwdBt = (Button) findViewById(R.id.save_pri_pwd_bt);



        savePriPwdBt.setOnClickListener(v -> {
            String pwd = priPwdEt.getText().toString().trim();
            String pwdAgain = priPedAgainEt.getText().toString().trim();

            if (pwd.equals("") || pwdAgain.equals("")){
                Toast.makeText(this, "请输入完整数据", Toast.LENGTH_SHORT).show();
                return;
            }
            if (!pwd.equals(pwdAgain)){
                Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return;
            }

            SharedPreferences.Editor data = getSharedPreferences("data", MODE_PRIVATE).edit();
            data.putString("PriPwd",pwd).apply();
            Toast.makeText(this, "保存成功！", Toast.LENGTH_SHORT).show();
            finish();
        });
    }
}